package NetWork.UDPDome002;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * ClassName:SendMassage
 * Package:NetWork.UDPDome002
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-20:44
 * @Version:v1.0
 */
public class SendMassage {
    /*UDP发送数据：数据来自于键盘录入,直到输入的数据是886,发送数结束
    * UDP接受数据：因为接收端不知道发送端什么时候停止发送,故采用死循环接收*/
    public static void main(String[] args) throws IOException {
        //创建DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();
        //打包数据
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入您要说的话");
            String str =sc.next();
            if ("886".equals(str)){
                break;
            }
            byte[] bytes = str.getBytes();

            //255.255.255.255
            InetAddress address = InetAddress.getByName("127.0.0.1");

            int port=10086;

            DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);

            //发送数据
            ds.send(dp);
        }

        //释放资源
        ds.close();

    }
}
